﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace PacoToolKit.Json
{
    /// <summary>
    /// 转换器（Json Xml转换）
    /// </summary>
    public static class XmlConvertor
    {
        /// <summary>
        /// Json转Xml
        /// </summary>
        /// <param name="json">Json字符串</param>
        /// <param name="rootElementName">根节点名称</param>
        /// <returns></returns>
        public static string JsonToXml(string json, string rootElementName)
        {
            System.Xml.Linq.XNode node = JsonConvert.DeserializeXNode(json, rootElementName);
            return node.ToString();
        }

        /// <summary>
        /// Xml转Json
        /// </summary>
        /// <param name="xml">xml字符串</param>
        /// <returns></returns>
        public static string XmlToJson(string xml)
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xml);
            return JsonConvert.SerializeXmlNode(doc);
        }
    }

}
